import Emitter from 'event-emitter';

const Mixin = {

    emitter: new Emitter(),

    hasClass: function(obj, cls) {
        return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
    },

    addClass: function(obj, cls) {
        if (!this.hasClass(obj, cls)) obj.className += " " + cls;
    },

    removeClass: function(obj, cls) {
        if (this.hasClass(obj, cls)) {
            const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
            obj.className = obj.className.replace(reg, ' ');
        }
    },
    // 获取网页滚动条宽度
    scrollWidth: function() {
        let noScroll, scroll, oDiv = document.createElement("DIV");
        oDiv.style.cssText = "position:absolute; top:-1000px; width:100px; height:100px; overflow:hidden;";
        noScroll = document.body.appendChild(oDiv).clientWidth;
        oDiv.style.overflowY = "scroll";
        scroll = oDiv.clientWidth;
        document.body.removeChild(oDiv);
        return noScroll - scroll;
    }(),

    trim: function(str) {
        return str.replace(/(^\s*)|(\s*$)/g, "");
    },

    ltrim: function(str) {
        return str.replace(/(^\s*)/g, "");
    },

    rtrim: function(str) {
        return str.replace(/(\s*$)/g, "");
    }
}


export default Mixin;